package com.lz.common;


import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public enum ResultEnum {
    /**
     * 成功返回结果，成功统一从10000开始.
     */
    SUCCESS(10000, "请求成功"),
    ERROR(20000, "请求失败"), //失败
    INVALID_PARAMETER(20001, "参数错误：文件或令牌为空或无效"),
    FILE_DELETE_FAILED(21000, "文件删除失败"),
    DB_UPDATE_FAILED(20002, "数据库更新失败"),
    UNKNOWN_ERROR(99999, "未知错误");
    private final Integer code;
    private final String message;

    ResultEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * description: 统一的返回对象.
     *
     * @param data 需要返回的数据
     * @return java.util.Map
     */
    private Result<Object> toResponseObject(Object data) {
        Result<Object> response = new Result<>();
        response.setFlag(this.code >= 10000);
        response.setCode(this.code);
        response.setMessage(this.message);
        if (data != null) {
            response.setData(data);
        }
        return response;
    }

    private Result<Object> toResponseObject(List<Object> dataList) {
        Result<Object> response = new Result<>();
        response.setFlag(this.code >= 10000);
        response.setCode(this.code);
        response.setMessage(this.message);
        if (dataList != null) {
            response.setData(dataList);
        }
        return response;
    }


    /**
     * description: toResponseResult.
     *
     * @return java.util.Map
     */
    public Result<Object> toResponseResult() {
        return toResponseObject("");
    }

    /**
     * description: toResponseResult.
     *
     * @param data 需要返回的数据
     * @return java.util.Map
     */
    public Result<Object> toResponseResult(Object data) {
        return toResponseObject(data);
    }

    public static ResultEnum getErrorCode(int code) {
        for (ResultEnum it : ResultEnum.values()) {
            if (it.getCode() == code) {
                return it;
            }
        }
        return UNKNOWN_ERROR;
    }
}

